﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Master/SiteContent.Master" AutoEventWireup="true" CodeBehind="Move.aspx.cs" Inherits="Warehouse.Web.Inventory.Move" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <style type="text/css">
        .infotable {
            width: 100%;
        }
        .datatable {
            width: 100%;
        }
        .datatable td {
            padding: 0;
        }
        .commoninput {
            width: 120px;
        }
        .datatable input {
            border: none;
            width: 100%;
            padding: 0 5px;
            height: 26px;
            line-height: 22px;
            background: Transparent;
        }
        .col_sn {
            width: 30px;
        }
        .col_product {
            width: 220px;
        }
        .col_unit {
            width: 30px;
            text-align: center;
        }
        .col_specification {
            width: 40px;
            text-align: center;
        }
        .col_count {
            width: 40px;
        }
        .col_price {
            width: 40px;
        }
        .col_amount {
            width: 60px;
            text-align: right;
        }
        .col_comments {
        }
    </style>
    <script type="text/javascript">
        var PRODUCTS = {};
        var PRODUCTSPECIFICATIONS = {};
        var WAREHOUSES = {};
        var SUPPLIERS = {};

        function saveStock() {
            var stock = Util.getFields(".stockinfo");
            if (!stock.SerialNumber) {
                alert("请输入编号");
                return;
            }
            if (!stock.MovingDate) {
                alert("请输入移库日期");
                return;
            }

            var detaillist = [];
            var err = "";
            $.each($(".detailstable tbody tr"), function () {
                var detail = Util.getFields(this);
                if (detail.ProductSpecificationID) {
                    if (isNaN(detail.Count)) {
                        err = "数量必须为数字";
                    } else {
                        detail.Count = detail.Count - 0;
                        detaillist.push(detail);
                    }
                }
            });
            if (err) {
                alert(err);
                return;
            }
            if (detaillist.length == 0) {
                alert("请输入产品");
                return;
            }
            var fromHouse = WAREHOUSES[stock.FromHouseID];
            if (!fromHouse) {
                alert("请选择移出仓库");
                resizeBy;
            }
            var toHouse = WAREHOUSES[stock.ToHouseID];
            if (!toHouse) {
                alert("请选择移入仓库");
                resizeBy;
            }
            var html = '<div class="dialog">' + widgets.buildMoveStockTable(stock, detaillist, fromHouse, toHouse) + '</div>';
            var dialog = top.showDialog(html, { title: '确认移库',
                resizable: false,
                modal: true,
                width: 600,
                buttons: {
                    "确认移库": function () {
                        Util.post("", { action: "SaveStock", Stock: JSON.stringify(stock), Details: JSON.stringify(detaillist) }, function (succeed, data, err) {
                            if (succeed) {
                                alert("保存成功");
                                dialog.dialog('close');
                                window.document.forms[0].reset();
                            } else {
                                alert(err);
                            }
                        }, { autoOverlay: true });
                    },
                    "取消": function () {
                        dialog.dialog('close');
                    }
                }
            });
        }

        function clearInput() {
            if (confirm('确认删除所有已输入数据？')) {
                $('[field="ProductSpecificationID"]').val('');
                document.forms[0].reset();
            }
        }

        $(function () {
            $(".datepicker").datepicker({ dateFormat: 'yy-mm-dd' });
            $.each(eval("(" + document.getElementById("allwarehouses").innerHTML + ")"), function () {
                WAREHOUSES[this.ID] = this;
            });
            var productnames = {};
            $.each(eval("(" + document.getElementById("allproducts").innerHTML + ")"), function () {
                PRODUCTS[this.ID] = this;
            });
            $.each(eval("(" + document.getElementById("allproductspecifications").innerHTML + ")"), function () {
                var p = PRODUCTS[this.ProductID];

                if (p) {
                    PRODUCTSPECIFICATIONS[this.ID] = this;
                    var item = { label: [p.Code || '', p.Name, p.Grade, p.Model, "[" + this.Specification + "]"].join(" "), value: [p.Name, p.Grade, p.Model].join(" "), Product: p, Specification: this };
                    productnames[this.ID] = item;
                }
            });
            $.each(eval("(" + document.getElementById("allsuppliers").innerHTML + ")"), function () {
                SUPPLIERS[this.ID] = this;
            });

            $('.col_product input[type="text"]').autocomplete({
                autoFocus: true,
                delay: 10,
                source: (function () { var arr = []; for (var k in productnames) { arr.push(productnames[k]); }; return arr; })(),
                select: function (event, ui) {
                    var tr = $(this).parents("tr").first();
                    tr.find('.col_specification input[type="text"]').val(ui.item.Specification.Specification);
                    tr.find('.col_unit input[type="text"]').val(ui.item.Specification.Unit);
                    tr.find('[field="ProductSpecificationID"]').val(ui.item.Specification.ID);
                }
            }).bind('blur', function () {
                var tr = $(this).parents("tr").first();
                var specificationId = tr.find('[field="ProductSpecificationID"]').val();
                var item = productnames[specificationId]
                if (!item || item.value != this.value) {
                    this.value = "";
                    var tr = $(this).parents("tr").first();
                    tr.find('.col_specification input[type="text"]').val('');
                    tr.find('.col_unit input[type="text"]').val('');
                    tr.find('.col_specification input[type="text"]').val('');
                    tr.find('[field="Count"]').val('');
                    tr.find('[field="Price"]').val('');
                    tr.find('[field="Amount"]').val('');
                    tr.find('[field="Comments"]').val('');
                    tr.find('[field="ProductSpecificationID"]').val('');
                }
            });

            $('[field="SerialNumber"]').focus();
        });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="moduleheader">
        <h4>
            移库</h4>
    </div>
    <div class="modulecontent">
        <table class="infotable stockinfo">
            <tr>
                <td>
                    <label>
                        移出仓库</label>
                    <asp:DropDownList ID="ddlFromWareHouse" runat="server" DataTextField="Name" DataValueField="ID"
                        field="FromHouseID">
                    </asp:DropDownList>
                </td>
                <td>
                    <label>
                        移入仓库</label>
                    <asp:DropDownList ID="ddlToWareHouse" runat="server" DataTextField="Name" DataValueField="ID"
                        field="ToHouseID">
                    </asp:DropDownList>
                </td>
                <td>
                    <label>
                        移库单编号</label>
                    <input type="text" field="SerialNumber" class="commoninput" />
                </td>
                <td>
                    <label>
                        入库日期</label>
                    <input type="text" field="MovingDate" class="datepicker" value='<%= DateTime.Now.ToString("yyyy-MM-dd") %>' />
                </td>
            </tr>
        </table>
        <table class="datatable detailstable">
            <thead>
                <tr>
                    <th class="col_sn">
                        编号
                    </th>
                    <th class="col_product">
                        产品名称
                    </th>
                    <th class="col_unit">
                        单位
                    </th>
                    <th class="col_specification">
                        规格
                    </th>
                    <th class="col_count">
                        数量
                    </th>
                    <th class="col_comments">
                        备注
                    </th>
                </tr>
            </thead>
            <tbody>
                <asp:Repeater ID="rptDetails" runat="server">
                    <ItemTemplate>
                        <tr>
                            <td class="col_sn">
                                <input type="hidden" field="ProductSpecificationID" />
                                <input type="text" field="SerailNumber" value="<%# Container.DataItem %>" />
                            </td>
                            <td class="col_product">
                                <input type="text" />
                            </td>
                            <td class="col_unit">
                                <input type="text" readonly="readonly" />
                            </td>
                            <td class="col_specification">
                                <input type="text" readonly="readonly" />
                            </td>
                            <td class="col_count">
                                <input type="text" field="Count" />
                            </td>
                            <td class="col_comments">
                                <input type="text" field="Comments" />
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </tbody>
        </table>
        <table class="infotable stockinfo">
            <tr>
                <td colspan="4" style="text-align: right;">
                    <uc:AuthorityControl ID="AuthorityControl1" runat="server" Action="SaveStock">
                        <input type="button" class="ui-button" onclick="saveStock();" value="保存" />
                        <input type="button" class="ui-button" onclick="clearInput();" value="清空" />
                    </uc:AuthorityControl>
                </td>
            </tr>
        </table>
    </div>
</asp:Content>
